package com.waterworld.watch.push.receiver;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.waterworld.watch.chat.activity.ChatActivity;
import com.waterworld.watch.home.activity.HomePagerActivity;
import com.waterworld.watch.mine.activity.NotificationActivity;
import com.waterworld.watch.push.contant.MessageContants;
import com.waterworld.watch.push.event.MessageEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

/**
 * Created by nhuan
 * Time:2019/1/31.
 */
public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
        if (extraMap.get("type") != null) {
            switch (extraMap.get("type")) {
                case "2":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.LOCATION,null));
                    break;
                case "3":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.STEP,null));
                    break;
                case "101":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.WATCH_BIND,null));
                    break;
                case "102":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.SECURITY_FENCE,null));
                    break;
                case "103":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.LOW_POWER,null));
                    break;
                case "104":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.SIM_PULL_OUT,null));
                    break;
                case "105":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.URGENT_CALL,null));
                    break;
                case "106":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.UPLOAD_PHOTO_OK,null));
                    break;
                case "107":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.UPLOAD_PHOTO_FAIL,null));
                    break;
                case "108":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.CHAT,Integer.parseInt(extraMap.get("groupId"))));
                    break;
                case "109":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.OTHER_USER_LOGIN,null));
                    break;
                case "111":
                    EventBus.getDefault().post(new MessageEvent(MessageContants.WATCH_BIND_OK,null));
                    break;

            }
        }
    }
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        Map<String,String> map = (Map<String, String>) JSONObject.parse(extraMap);
        if (map.get("type").equals("108")) {
            context.startActivity(new Intent(context, HomePagerActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }else {
            Intent intent = new Intent(context, NotificationActivity.class);
            context.startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("title", title).putExtra("summary", summary).putExtra("extraMap", extraMap));
        }
    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }
}
